package me.wangqu.weiyouqu.web.rest.business.dao.impl;

import me.wangqu.weiyouqu.web.rest.business.dao.CashAccountDao;
import me.wangqu.weiyouqu.web.rest.business.model.CashAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by arcticfox on 6/27/16.
 */
@Repository
public class CashAccountDaoImpl implements CashAccountDao {

    private final Logger log = LoggerFactory.getLogger(CashAccountDaoImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public CashAccount getSelfCashAccount(String login) {
        log.debug(">>>>>>>> 获取用户帐户信息");
        if (!ObjectUtils.isEmpty(login)) {
            StringBuffer sql = new StringBuffer(256);
            List<Object> params = new ArrayList<Object>();
            sql.append("SELECT ");
            sql.append("    u.id AS uid,");
            sql.append("    u.phone AS login,");
            sql.append("    u.money AS balance,");
            sql.append("    u.income AS income,");
            sql.append("    IFNULL((SELECT ");
            sql.append("                    SUM(r.money)");
            sql.append("                FROM");
            sql.append("                    refererdata r");
            sql.append("                WHERE");
            sql.append("                    r.uid = u.id AND r.day = CURDATE()),");
            sql.append("            0) AS today,");
            sql.append("    IFNULL((SELECT ");
            sql.append("                    SUM(r.money)");
            sql.append("                FROM");
            sql.append("                    refererdata r");
            sql.append("                WHERE");
            sql.append("                    r.uid = u.id AND r.day = DATE_SUB(CURDATE(), INTERVAL 1 DAY)),");
            sql.append("            0) AS yesterday,");
            sql.append("    IFNULL((SELECT ");
            sql.append("                    SUM(r.money)");
            sql.append("                FROM");
            sql.append("                    refererdata r");
            sql.append("                WHERE");
            sql.append("                    r.uid = u.id AND r.day = CURDATE()");
            sql.append("                        AND r.aid = - 1),");
            sql.append("            0) AS stuIncome,");
            sql.append("    (SELECT ");
            sql.append("            COUNT(u.id)");
            sql.append("        FROM");
            sql.append("            userdata u");
            sql.append("        WHERE");
            sql.append("            u.tj_id = u.id) AS student");
            sql.append(" FROM");
            sql.append("    userdata u");
            sql.append(" WHERE");
            sql.append("    u.phone = ?");
            sql.append(" LIMIT 1");
            params.add(login);

            return jdbcTemplate.queryForObject(sql.toString(), params.toArray(), new RowMapper<CashAccount>() {
                @Override
                public CashAccount mapRow(ResultSet rs, int i) throws SQLException {
                    CashAccount ca = new CashAccount();
                    ca.setUid(rs.getLong("uid"));
                    ca.setLogin(rs.getString("login"));
                    ca.setBalance(rs.getBigDecimal("balance"));
                    ca.setIncome(rs.getBigDecimal("income"));
                    ca.setToday(rs.getBigDecimal("today"));
                    ca.setYesterday(rs.getBigDecimal("yesterday"));
                    ca.setStudent(rs.getInt("student"));
                    ca.setStuIncome(rs.getBigDecimal("stuIncome"));
                    return ca;
                }
            });
        }
        return null;
    }
}
